package com.buanalintas.sandals.pos.distributiondetail;

import com.buanalintas.bynga.Action;
import com.buanalintas.bynga.Dictionary;
import com.buanalintas.bynga.Field;
import com.buanalintas.bynga.Page;
import com.buanalintas.sandals.pos.entities.PosMasterItemStock;
import com.buanalintas.sandals.pos.entities.PosMasterSize;
import com.buanalintas.sandals.pos.entities.PosMasterStatus;
import com.buanalintas.sandals.pos.entities.PosTransactionDistributionDetail;
import com.buanalintas.sandals.pos.item.Item;
import com.buanalintas.sandals.pos.distributiondetail.AddSize;
import com.buanalintas.sandals.pos.status.Status;
import javax.persistence.EntityManager;

public class DistributionDetail extends Page {

    private Field distributionDetailId;
    private Field distributionDetailDistributionId;
    private Field distributionDetailItemId;
    private Field distributionDetailSizeId;
    private Field distributionDetailSize;
    private Field distributionDetailQuantity;
//    private Field distributionDetailPrice;
    private Field distributionDetailDescription;

    public DistributionDetail() {
        super();
        setTheClass(PosTransactionDistributionDetail.class);

        distributionDetailId = new Field();
        distributionDetailId.setName("distributionDetailId");
        distributionDetailId.setCaption("id");
        distributionDetailId.setListProperties("");
        distributionDetailId.setInputProperties("");
        getFields().add(distributionDetailId);

        distributionDetailDistributionId = new Field();
        distributionDetailDistributionId.setName("distributionDetailDistributionId");
        distributionDetailDistributionId.setCaption("Distribution");
        distributionDetailDistributionId.setListProperties(" align=\"right\" width=\"5%\" nowrap");
        distributionDetailDistributionId.setInputProperties(" size=\"3\"");
        distributionDetailDistributionId.setRequired(true);
        getFields().add(distributionDetailDistributionId);
        
        distributionDetailItemId = new Field();
        distributionDetailItemId.setName("distributionDetailItemId");
        distributionDetailItemId.setCaption("Item");
        distributionDetailItemId.setListProperties(" align=\"left\" width=\"20%\" nowrap");
        distributionDetailItemId.setInputProperties(" size=\"25\"");
        distributionDetailItemId.setRequired(true);
        distributionDetailItemId.getSelectorColumns().add(new String[]{"itemCode", " Code", "width=\"5%\" nowrap"});
        distributionDetailItemId.getSelectorColumns().add(new String[]{"itemModel", " Model", "width=\"90%\""});
        distributionDetailItemId.getViewColumns().addAll(distributionDetailItemId.getSelectorColumns());
        distributionDetailItemId.setSelectorOrder("t.itemId");
        getFields().add(distributionDetailItemId);
        
        distributionDetailSize = new Field();
        distributionDetailSize.setName("distributionDetailSize");
        distributionDetailSize.setCaption("size");
        distributionDetailSize.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        distributionDetailSize.setEntityRelation(PosMasterSize.class);
        distributionDetailSize.setInputType(Field.INPUT_CHECK_BOX);
        distributionDetailSize.setRequired(true);
        getFields().add(distributionDetailSize);

        distributionDetailSizeId = new Field();
        distributionDetailSizeId.setName("distributionDetailSizeId");
        distributionDetailSizeId.setCaption("size");
        distributionDetailSizeId.setListProperties(" align=\"left\" width=\"30%\" nowrap");
        //purchaseOrderDetailSizeId.setInputProperties(" size=\"30\"");
        distributionDetailSizeId.setEntityRelation(PosMasterSize.class);
        //purchaseOrderDetailSizeId.setInputType(Field.INPUT_CHECK_BOX);
        distributionDetailSizeId.setRequired(true);
        getFields().add(distributionDetailSizeId);

        distributionDetailQuantity = new Field();
        distributionDetailQuantity.setName("distributionDetailQuantity");
        distributionDetailQuantity.setCaption("Quantity");
        distributionDetailQuantity.setListProperties(" align=\"right\" width=\"5%\" nowrap");
        distributionDetailQuantity.setInputProperties(" size=\"5\"");
        getFields().add(distributionDetailQuantity);
        
//        distributionDetailPrice = new Field();
//        distributionDetailPrice.setName("distributionDetailPrice");
//        distributionDetailPrice.setCaption("price");
//        distributionDetailPrice.setListProperties(" align=\"right\" width=\"10%\" nowrap");
//        distributionDetailPrice.setInputProperties(" size=\"8\"");
//        distributionDetailPrice.setRequired(true);
//        distributionDetailPrice.setFormater(MoneyFormatter.getInstance());
//        getFields().add(distributionDetailPrice);

        distributionDetailDescription = new Field();
        distributionDetailDescription.setName("distributionDetailDescription");
        distributionDetailDescription.setCaption("description");
        distributionDetailDescription.setListProperties(" width=\"70%\"");
        distributionDetailDescription.setInputProperties(" cols=\"50\" rows=\"5\"");
        distributionDetailDescription.setInputType(Field.INPUT_TEXT_AREA);
        distributionDetailDescription.setInputCase(Field.CASE_UPPER);
        getFields().add(distributionDetailDescription);

        fillFields();

        getActions().add(Action.TRANS_DETAIL_LIST);
        getActions().add(Action.TRANS_DETAIL_ADD);
        getActions().add(Action.TRANS_DETAIL_EDIT);

        getFieldsAdd().clear();
        getFieldsAdd().add(distributionDetailItemId);
        getFieldsAdd().add(distributionDetailSize);
        getFieldsAdd().add(distributionDetailQuantity);
        getFieldsAdd().add(distributionDetailDescription);

        getFieldsEdit().clear();
        getFieldsEdit().add(distributionDetailItemId);
        getFieldsEdit().add(distributionDetailSizeId);
        getFieldsEdit().add(distributionDetailQuantity);
        getFieldsEdit().add(distributionDetailDescription);

        getFieldsList().clear();
        getFieldsList().add(distributionDetailItemId);
        getFieldsList().add(distributionDetailSizeId);
        getFieldsList().add(distributionDetailQuantity);
        getFieldsList().add(distributionDetailDescription);
    }
    
    @Override
    public StringBuffer process() {
        if (getAction().equals(Action.TRANS_DETAIL_ADD)) {
            return AddSize.process(this);
        }
//        if (getAction().equals(Action.TRANS_DETAIL_LIST)) {
//            return ListTransaction.process(this);
//        }
        return super.process();
    }

    @Override
    public String validate(Object data) {
        EntityManager em = getEntityManager();
        PosMasterStatus statusGood = Status.getStatusGood(em);
        PosTransactionDistributionDetail distributionDetail = (PosTransactionDistributionDetail) data;
        PosMasterItemStock stock = Item.getStock(em, distributionDetail.getDistributionDetailItemId(), getCurrentUnit(), statusGood, distributionDetail.getDistributionDetailItemId().getItemColorId(), distributionDetail.getDistributionDetailSizeId());
        if (distributionDetail.getDistributionDetailQuantity().compareTo(stock.getItemStockValue()) > 0) {
            return distributionDetail.getDistributionDetailItemId() + " " + Dictionary.getValue(this, "insufficient stock") + ", " + Dictionary.getValue(this, "current stock: ") + ": " + stock.getItemStockValue() + " " + Dictionary.getValue(this, "will be distribution") + ": " + distributionDetail.getDistributionDetailQuantity();
        }
        return null;
    }
}
